package org.moonholder.springboot.damocles.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.moonholder.springboot.damocles.entity.Daily;
import org.moonholder.springboot.damocles.entity.User;
import org.moonholder.springboot.damocles.entity.vo.ResponseEntity;
import org.moonholder.springboot.damocles.service.FileTransferService;
import org.moonholder.springboot.damocles.service.IDailyService;
import org.moonholder.springboot.damocles.util.GenerateDoc;
import org.moonholder.springboot.damocles.util.JwtUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author MoonHolder
 * @since 2020-12-19
 */
@RestController
@RequestMapping("/daily")
public class DailyController {
    @Resource
    private IDailyService dailyService;
    @Resource
    private JwtUtil jwtUtil;
    @Resource
    private UserDetailsService userDetailsService;
    @Resource
    private FileTransferService fileTransferService;

    @GetMapping("record/read")
    @PreAuthorize("hasAuthority('daily:record:read')")
    public ResponseEntity queryRecord(HttpServletRequest request, Daily daily, Page<Daily> page) {
        String username = jwtUtil.getUsernameFromRequest(request);
        User user = (User) userDetailsService.loadUserByUsername(username);
        daily.setUserId(user.getId());
        return ResponseEntity.success(dailyService.findDailyPageByCondition(page, daily));
    }

    @PostMapping("record/create")
    @PreAuthorize("hasAuthority('daily:record:create')")
    public ResponseEntity createRecord(HttpServletRequest request, @RequestBody Daily daily) {
        String username = jwtUtil.getUsernameFromRequest(request);
        User user = (User) userDetailsService.loadUserByUsername(username);
        daily.setUserId(user.getId());
        daily.setStatus("draft");
        return dailyService.save(daily) ? ResponseEntity.success() : ResponseEntity.error();
    }

    @PostMapping("record/update")
    @PreAuthorize("hasAuthority('daily:record:update')")
    public ResponseEntity updateRecord(@RequestBody Daily daily) {
        return dailyService.updateById(daily) ? ResponseEntity.success() : ResponseEntity.error();
    }

    @DeleteMapping("record/delete")
    @PreAuthorize("hasAuthority('daily:record:delete')")
    public ResponseEntity deleteRecord(Integer dailyId) {
        return dailyService.removeById(dailyId) ? ResponseEntity.success() : ResponseEntity.error();
    }

    @GetMapping("checkRecord")
    @PreAuthorize("hasAuthority('daily:checkRecord')")
    public ResponseEntity checkRecord(Page<Daily> page) {
        return ResponseEntity.success(dailyService.page(page, Wrappers.lambdaQuery(Daily.class).eq(Daily::getStatus, "submitted")));
    }

    @PostMapping("checkRecord/check")
    @PreAuthorize("hasAuthority('daily:checkRecord:check')")
    public ResponseEntity checkRecord(@RequestBody Daily daily) {
        return dailyService.updateById(daily) ? ResponseEntity.success() : ResponseEntity.error();
    }

    @GetMapping("list/query")
    @PreAuthorize("hasAuthority('daily:list:query')")
    public ResponseEntity checkRecord(Daily daily, Page<Daily> page) {
        return ResponseEntity.success(dailyService.findDailyPageByCondition(page, daily));
    }

    @PostMapping("list/doc")
    @PreAuthorize("hasAuthority('daily:list:doc')")
    public void dailyDoc(@RequestBody Daily daily, HttpServletResponse response) {
        String fileName = GenerateDoc.generating(daily);
        fileTransferService.download(fileName, response);
    }

    @GetMapping("/userRecord")
    @PreAuthorize("hasAuthority('daily:userRecord')")
    public ResponseEntity userRecord(User user, Page<User> page) {
        return ResponseEntity.success(dailyService.findDailySubmitRecord(page, user));
    }
}
